#!/bin/sh
# PCP QA Test No. 1139
# Ranking, sorting, limit filter, and predicates tests with pmrep.
#
# Copyright (c) 2018-2019 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

status=1        # failure is the default!
hostname=`hostname`
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_msg_filter()
{
    sed \
       -e "s,Recording .* metrics.*,Recording metrics...,g" \
    #end
}

log="--archive $here/archives/rank-pred -z"
out="-o archive -F $tmp.archive"

# real QA test starts here
echo "== test ranking"
pmrep -s  7 $log -p -r -u -J  2 disk.dev.read mem.util.used
pmrep -s  7 $log -p -r -u -J -3 disk.dev.read kernel.pernode.cpu.user

echo "== test limit filtering"
pmrep -s 14 $log -p -r -u -9  5 disk.dev.read disk.dev.write
pmrep -s 14 $log -p -r -u -9 -5 disk.dev.read disk.dev.write

echo "== test predicate"
pmrep -s 14 $log -p -r -u -J  2 -N disk.dev.read disk.dev.read disk.dev.write mem.util.used
pmrep -s 14 $log -p -r -u -J  1 -N disk.dev.read,kernel.pernode.cpu.sys \
  disk.dev.read disk.dev.write mem.util.used kernel.pernode.cpu.user kernel.pernode.cpu.sys

echo "== test predicate with limit filtering"
pmrep -s 14 $log -p -r -u -J  2 -8  10 -N disk.dev.read disk.dev.read disk.dev.write mem.util.used
pmrep -s 14 $log -p -r -u -J  2 -8 -10 -N disk.dev.read disk.dev.read disk.dev.write mem.util.used

echo "== test predicate with limit filtering with dynamic header and predicate values above/below limit"
pmrep -s  4 $log -p -r -u -J  2 -8  50 -N disk.dev.read disk.dev.read disk.dev.write mem.util.used \
  -S @00:00:14 -1
pmrep -s  4 $log -p -r -u -J  2 -8 -50 -N disk.dev.read disk.dev.read disk.dev.write mem.util.used \
  -S @00:00:14 -1

echo "== test instance filter, live filter, invert filter, ranking, predicate"
pmrep -s 14 $log $out -p -r -u                          disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2                 disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2 -i sda          disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2 -i sda -j       disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2 -i sda -j -n    disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2 -i sda -j -n -N disk.dev.read disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2           -n -N disk.dev.read disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta
pmrep -z -s 14 $log $out -p -r -u -J  2              -N disk.dev.read disk.dev.read disk.dev.write | \
_msg_filter
pmrep -z -a $tmp.archive -p -r -u disk
rm -f $tmp.archive.0 $tmp.archive.index $tmp.archive.meta

echo "== test predicate being filtered out"
pmrep -z -s 14 $log $out -p -r -u -J  2 -i sda       -N kernel.pernode.cpu.sys disk.dev.write | \
_msg_filter

echo "== test predicate with sort metric"
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1                  -6  kernel.pernode.cpu.sys disk.dev.read disk.dev.write
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1 -N disk.dev.read -6  mem.util.used          disk.dev.read disk.dev.write mem.util.used
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1 -N disk.dev.read -6  disk.dev.read          disk.dev.read disk.dev.write
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1 -N disk.dev.read -6 -disk.dev.read          disk.dev.read disk.dev.write
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1 -N disk.dev.read -6  disk.dev.write         disk.dev.read disk.dev.write
pmrep -s 2 -S @00:00:15 $log -p -r -u -J 3 -X "" -i sdb,sdc,sdd -1 -N disk.dev.read -6 -disk.dev.write         disk.dev.read disk.dev.write

echo "== test overall ranking"
pmrep $log -I -s 12 -r -u   -J 2 -l - -2 disk kernel mem
pmrep $log -I -s  6 -r -t 2 -J 2 -l X -2 disk kernel mem
pmrep $log -I -s 12 -r -u   -J 2 -l - -3 disk mem kernel
pmrep $log -I -s  6 -r -t 2 -J 2 -l X -3 mem disk kernel

# success, all done
status=0
exit
